~ chicken-core (master) /manual/Module (chicken port)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken port)
  5
  6This module contains various extended port definitions.
  7
  8All errors related to failing port operations will signal a condition
  9of kind {{exn}}.
 10
 11* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
 12  change errno will produce a condition object with an {{errno}}
 13  property, which can be accessed with
 14  {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
 15
 16=== Port attributes
 17
 18==== port-encoding
 19
 20<procedure>(port-encoding PORT)</procedure>
 21
 22Returns the encoding used for reading and writing data from and to
 23the given port. Encoding is currently one of the symbols {{utf-8}},
 24{{latin-1}} or {{binary}}. Note that the encoding can be changed by using
 25the (SRFI-17) setter procedure for this operation:
 26
 27{{(set! (port-encoding PORT) ENCODING)}}
 28
 29==== port-name
 30
 31<procedure>(port-name [PORT])</procedure>
 32
 33Fetch filename from {{PORT}}. This returns the filename that was used to open
 34this file. Returns a special tag string, enclosed into parentheses for
 35non-file ports. {{PORT}} defaults to the value of {{(current-input-port)}}.
 36
 37Note that the encoding can be changed by using
 38the (SRFI-17) setter procedure for this operation:
 39
 40{{(set! (port-name PORT) STRING)}}
 41
 42
 43==== port-position
 44
 45<procedure>(port-position [PORT])</procedure>
 46
 47Returns the current position of {{PORT}} as two values: row and column number.
 48If the port does not support such an operation an error is signaled. This
 49procedure is currently only available for input ports. {{PORT}} defaults to the
 50value of {{(current-input-port)}}.
 51
 52
 53=== Setting the file buffering mode
 54
 55==== set-buffering-mode!
 56
 57<procedure>(set-buffering-mode! PORT MODE [BUFSIZE])</procedure>
 58
 59Sets the buffering-mode for the file associated with {{PORT}} to
 60{{MODE}}, which should be one of the keywords {{#:full}},
 61{{#:line}} or {{#:none}}. If {{BUFSIZE}} is specified it
 62determines the size of the buffer to be used (if any).
 63
 64This procedure doesn't work on custom ports, such as those created with {{make-input-port}} or {{make-output-port}}.
 65
 66=== Terminal ports
 67
 68==== terminal-name
 69
 70<procedure>(terminal-name PORT)</procedure>
 71
 72Returns the name of the terminal that is connected to {{PORT}}.
 73
 74On Windows, this procedure always raises an exception.
 75
 76==== terminal-port?
 77
 78<procedure>(terminal-port? PORT)</procedure>
 79
 80Returns {{#t}} if {{PORT}} is connected to a terminal and
 81{{#f}} otherwise.
 82
 83
 84==== terminal-size
 85
 86<procedure>(terminal-size PORT)</procedure>
 87
 88Returns two values, the number of rows and columns of the terminal
 89that is connected to {{PORT}} or {{0}}, {{0}} if the terminal size can
 90not be obtained.
 91
 92On Windows, this procedure always raises an exception.
 93
 94
 95=== Input/output port extensions
 96
 97==== with-output-to-port
 98
 99<procedure>(with-output-to-port PORT THUNK)</procedure>
100
101Call procedure {{THUNK}} with the current output-port temporarily
102bound to {{PORT}}.
103
104==== make-input-port
105==== make-binary-input-port
106
107<procedure>(make-input-port READ-CHAR CHAR-READY? CLOSE #!key peek-char read-bytevector read-line)</procedure>
108<procedure>(make-binary-input-port READ-U8 U8-READY? CLOSE #!key peek-u8 read-bytevector)</procedure>
109
110Returns a custom input port. Common operations on this port are
111handled by the given parameters, which should be procedures of no
112arguments.  The following arguments are all different kinds of reader
113procedures:
114
115* {{READ-CHAR}} is the most fundamental reader, and must always be
116present.  It is a thunk which is called when the next character is
117to be read and it should return a character or {{#!eof}}.
118* {{READ-U8}} similar to {{READ-CHAR}} but reads and returns a byte.
119* {{CHAR-READY?}}/{{U8-READY?}} are thunks which are called when {{char-ready?}}
120or {{u8-ready?}} are called on this port and should return {{#t}} or {{#f}}.
121* {{CLOSE}} is a thunk which is called when the port is closed.
122* {{peek-char}}/{{peek-u8}} are thunks which are called when {{peek-char}} 
123or {{peek-u8}} are
124called on this port and should return a character/byte or {{#!eof}}. If
125not provided or {{#f}}, {{READ-CHAR}}/{{READ-U8}} will be used instead and the
126created port object handles peeking automatically (by calling the reader procedure
127and buffering the character).
128* {{read-bytevector}} is called when {{read-bytevector[!]}} or {{read-string[!]}} is called (or the
129higher-level non-mutating {{read-string}} and {{read-bytevector}}).  It will be invoked with 3
130arguments: a bytevector to read into (which may be
131assumed to be big enough to hold the data) and the offsets of the
132starting and ending position into the
133buffer at which to put the data to read.  It should return the number
134of bytes that have successfully been read, which should always be
135equal to the requested bytes unless EOF was hit, in which case it can
136be less.  If this procedure is not provided or {{#f}}, the buffer will
137be filled by repeated reads to {{READ-CHAR}}.
138* {{READ-LINE}} is called when {{read-line}} is called.  It will be
139invoked with two arguments: the port created by {{make-input-port}}
140and the maximum number of characters to read (or {{#f}}).  If this
141procedure is not provided or {{#f}}, the buffer will be filled by
142repeated reads to {{READ-CHAR}}.
143
144All the optional procedures except for {{PEEK-CHAR}} are responsible
145for updating the port's position, which currently can only be done via
146low-level slot accessors like {{##sys#setslot}}; slot 4 is the row
147number (ie, the line) and slot 5 is the column number (ie, the
148character on the line).  If the port's positions are not updated,
149{{port-position}} won't work.
150
151Note that reading binary input from a custom non-binary input port is only possible
152when the {{read-bytevector}} operation is given, as byte-input can
153currently not ben synthesized from character-input operations.
154
155
156==== make-output-port
157==== make-binary-output-port
158
159<procedure>(make-output-port WRITE CLOSE #!key force-output)</procedure>
160<procedure>(make-binary-output-port WRITE CLOSE #!key force-output write-bytevector)</procedure>
161
162Returns a custom output port. Common operations on this port are handled
163by the given parameters, which should be procedures.  {{WRITE}} is
164called when output is sent to the port and receives a single argument,
165a string for {{make-output-port}} or an integer for {{make-binary-output-port}}.  
166{{CLOSE}} is called when the port is closed and should
167be a procedure of no arguments. {{force-output}} (if provided) is called
168for flushing the output port. The optional {{write-bytevector}} allows
169more optimized writing of partial buffers and takes 3 arguments: a
170bytevector, a starting position and an ending position.
171
172
173==== with-error-output-to-port
174
175<procedure>(with-error-output-to-port PORT THUNK)</procedure>
176
177Call procedure {{THUNK}} with the current error output-port
178temporarily bound to {{PORT}}.
179
180
181==== with-input-from-port
182
183<procedure>(with-input-from-port PORT THUNK)</procedure>
184
185Call procedure {{THUNK}} with the current input-port temporarily
186bound to {{PORT}}.
187
188
189=== String-port extensions
190
191==== call-with-input-string
192
193<procedure>(call-with-input-string STRING PROC)</procedure>
194
195Calls the procedure {{PROC}} with a single argument that is a
196string-input-port with the contents of {{STRING}}.
197
198
199==== call-with-output-string
200
201<procedure>(call-with-output-string PROC)</procedure>
202
203Calls the procedure {{PROC}} with a single argument that is a
204string-output-port.  Returns the accumulated output-string.
205
206
207==== with-input-from-string
208
209<procedure>(with-input-from-string STRING THUNK)</procedure>
210
211Call procedure {{THUNK}} with the current input-port temporarily
212bound to an input-string-port with the contents of {{STRING}}.
213
214
215==== with-output-to-string
216
217<procedure>(with-output-to-string THUNK)</procedure>
218
219Call procedure {{THUNK}} with the current output-port temporarily
220bound to a string-output-port and return the accumulated output string.
221
222==== with-error-output-to-string
223
224<procedure>(with-error-output-to-string THUNK)</procedure>
225
226Call procedure {{THUNK}} with the current error output-port
227temporarily bound to a string-output-port and return the accumulated
228output string.
229
230
231=== Port iterators
232
233==== port-for-each
234
235<procedure>(port-for-each FN THUNK)</procedure>
236
237Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, discarding the results.
238
239==== port-map
240
241<procedure>(port-map FN THUNK)</procedure>
242
243Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, returning a list of the collected results.
244
245==== port-fold
246
247<procedure>(port-fold FN ACC THUNK)</procedure>
248
249Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}}, (typically {{read}}) passing the {{ACC}} value as the second argument. The {{FN}} result becomes the new {{ACC}} value. When {{THUNK}} returns {{#!eof}}, the last {{FN}} result is returned.
250
251==== copy-port
252
253<procedure>(copy-port FROM TO [READ [WRITE]])</procedure>
254
255Reads all remaining data from port {{FROM}} using the reader procedure
256{{READ}} and writes it to port {{TO}} using the writer procedure
257{{WRITE}}. {{READ}} defaults to {{read-char}} and {{WRITE}} to
258{{write-char}}. Note that this procedure does not check {{FROM}} and
259{{TO}} for being ports, so the reader and writer procedures may
260perform arbitrary operations as long as they can be invoked
261as {{(READ FROM)}} and {{(WRITE X TO)}}, respectively.
262{{copy-port}} returns an undefined value.
263
264{{copy-port}} was introduced in CHICKEN 4.6.0.
265
266=== Funky ports
267
268==== make-bidirectional-port
269
270<procedure>(make-bidirectional-port INPUT-PORT OUTPUT-PORT)</procedure>
271
272Returns a joint input/output port that proxies port operations to the
273given {{INPUT-PORT}} and {{OUTPUT-PORT}}, respectively. This port
274satisfies both {{input-port?}} and {{output-port?}}, and its two
275directions may be closed independently.
276
277==== make-broadcast-port
278
279<procedure>(make-broadcast-port PORT ...)</procedure>
280
281Returns a custom output port that emits everything written into it to
282the ports given as {{PORT ...}}. Closing the broadcast port does not close
283any of the argument ports.
284
285==== make-concatenated-port
286
287<procedure>(make-concatenated-port PORT1 PORT2 ...)</procedure>
288
289Returns a custom input port that reads its input from {{PORT1}}, until it
290is empty, then from {{PORT2}} and so on. Closing the concatenated port
291does not close any of the argument ports.
292
293
294---
295Previous: [[Module (chicken plist)]]
296
297Next: [[Module (chicken pretty-print)]]
Trap